<template>
  <div class="setting-container">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理" />
      </el-tabs>
      <el-row>
        <el-button type="primary" @click="addRole">+新建角色</el-button>
      </el-row>
      <el-table :data="list">
        <el-table-column label="序号" prop="" type="index" />
        <el-table-column label="角色名称" prop="name" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
            <el-button size="small" type="primary" @click="doEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="doDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        small
        layout="prev, pager, next"
        :total="total"
        style="text-align:center"
        :page-size="pages.pagesize"
        :current-page="pages.page"
        @current-change="pageChange"
      />

      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ? '编辑': '新增'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="doSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 新增权限弹框 -->
      <el-dialog title="分配权限" :visible.sync="showPermission">
        <assign-permission
          ref="perRef"
          :cur-id="curId"
          @close="close"
          @success="success"
        />
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { sendRoles, sendDelete, sendAddRole, sendEditRole } from '@/api/settings'
import assignPermission from '@/views/settings/assignPermission.vue'
export default {
  components: { assignPermission },
  data() {
    return {
      list: [],
      // 页面参数
      pages: {
        // 当前页
        page: 1,
        // 每页条数
        pagesize: 30
      },
      // 总条数
      total: 1,
      showDialog: false,
      isEdit: false,
      showPermission: false,
      roleForm: {
        name: '',
        description: ''
      },
      curId: '',
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    // 获取角色信息
    async getRoles() {
      const res = await sendRoles(this.pages)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 页面更新调用函数，参数是当前页面
    pageChange(page) {
      this.pages.page = page
      this.getRoles()
    },
    // 删除数据
    doDelete(id) {
      this.$confirm('是否退出登录', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await sendDelete(id)
        if (this.list.length === 1) {
          this.pages.page--
          this.pages.page = Math.max(1, this.pages.page)
        }
        this.getRoles()
        this.$message.success(res.message)
      }).catch(() => {
      })
    },
    // 添加角色弹框
    addRole() {
      this.isEdit = false
      this.showDialog = true
    },
    // 添加角色ajax
    async doAddRole() {
      const res = await sendAddRole(this.roleForm)
      this.showDialog = false
      this.$message.success(res.message)
      this.total++
      this.pages.page = Math.ceil(this.total / this.pages.pagesize)
      this.getRoles()
    },
    // 编辑角色弹框
    doEdit(row) {
      this.roleForm = { ...row }
      this.showDialog = true
      this.isEdit = true
    },
    doSubmit() {
      if (this.isEdit) {
        this.editRole()
      } else {
        this.doAddRole()
      }
    },
    // 编辑角色
    async editRole() {
      const res = await sendEditRole(this.roleForm)
      this.showDialog = false
      this.$message.success(res.message)
      console.log(res)
      this.getRoles()
    },
    // 新增权限
    hAssign(id) {
      this.showPermission = true
      this.curId = id
      this.$nextTick(() => {
        this.$refs.perRef.loadPermissionList()
      })
    },
    // 关闭权限弹窗
    close() {
      this.showPermission = false
    },
    success() {
      this.showPermission = false
    }
  }

}
</script>

<style>
.setting-container{
  margin: 40px;
}
</style>
